package com.huizhonglm.diap.ui.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.ToastUtil;

/**
 * 图片选择弹出框
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-10-22 13:23:55
 */
public class ImageChooseView extends PopupWindow implements OnKeyListener,
        PopupWindow.OnDismissListener {
    private Context mContext;
    private View parent = null;
    private PopupMaskView mask;
    private OnImageChooseListener mListener;

    public ImageChooseView(Activity activity, View parent, OnImageChooseListener lis) {
        super(activity);
        this.parent = parent;
        this.mContext = activity;
        this.mListener = lis;
        mask = new PopupMaskView(activity, this);
        View mView = View.inflate(activity, R.layout.view_image_choose, null);
        Button cameraBtn = (Button) mView.findViewById(R.id.camera_btn);
        Button galleryBtn = (Button) mView.findViewById(R.id.gallery_btn);
        MyOnClickListener onClickListener = new MyOnClickListener();
        cameraBtn.setOnClickListener(onClickListener);
        galleryBtn.setOnClickListener(onClickListener);
        mView.setFocusableInTouchMode(true);
        mView.setOnKeyListener(this);
        setContentView(mView);
        setWidth(LayoutParams.MATCH_PARENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setFocusable(true);
        setAnimationStyle(R.style.bottom_view_anim_style);
        setOnDismissListener(this);
        update();
    }

    public void show() {
        if (!FileUtil.isSdcardMounted()) {
            ToastUtil.makeTextShort("无SD卡");
            return;
        }
        mask.showAtLocation(parent);
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    public interface OnImageChooseListener {
        void onCamera();

        void onGallery();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_MENU) && (isShowing())) {
            dismiss();
            return true;
        }
        return false;
    }

    @Override
    public void onDismiss() {
        mask.dismiss();
    }

    public class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.camera_btn:
                    if (mListener != null) {
                        mListener.onCamera();
                    }
                    break;
                case R.id.gallery_btn:
                    if (mListener != null) {
                        mListener.onGallery();
                    }

                    break;
            }
            dismiss();
        }
    }
}
